package com.gpipe.dirwatcherRealtime;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.gpipe.dbwriter.SendResultData;
import com.gpipe.dirwatcher.DirectorySnapshot;
import com.gpipe.properties.InputProperties;
import com.gpipe.tools.GpipeInit;

public class ThreadRealtimeNbrCome extends WriterThread {

	public ThreadRealtimeNbrCome(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doWriting() {
		// TODO Auto-generated method stub

//		// nbr|lac|ci:starttime|endtime
//		Map<String, String> midBaseNbr1 = new HashMap();
//
//		int startTimeIndex = 11;
//		int lacIndex = 9;
//		int ciIndex = 10;
//		int nbrIndex = 5;
//
//		Map<String, String> _files = DirectorySnapshotRealtime.oneHourFiles;
//		Collection<String> c = _files.values();
//		Iterator it = c.iterator();
//
//		while (it.hasNext()) {
//			String _fileName = it.next().toString();
//			BufferedReader reader = null;
//			try {
//				reader = new BufferedReader(new InputStreamReader(
//						new FileInputStream(_fileName)));
//			} catch (FileNotFoundException e) {
//				e.printStackTrace();
//			}
//			String line = null;
//			try {
//				while ((line = reader.readLine()) != null) {
//					String[] fields = null;
//					fields = line.split("\\|", -1);
//					if (fields.length >= 11) {
//						if (GpipeInit.isNotEmpty(fields[nbrIndex])) {
//							String key1 = fields[nbrIndex] + "|"+ fields[lacIndex] + "|" + fields[ciIndex];
//							if (midBaseNbr1.containsKey(key1)) {
//								String value1 = midBaseNbr1.get(key1);
//								value1 = start2endTime(value1,
//										fields[startTimeIndex]);
//								midBaseNbr1.put(key1, value1);
//							} else {
////								if(fields[startTimeIndex]==null){
////									System.out.println(key1+":"+fields[startTimeIndex]
////										+ "|" + fields[startTimeIndex]);
////								}
//								midBaseNbr1.put(key1, fields[startTimeIndex]
//										+ "|" + fields[startTimeIndex]);
//							}
//						}
//					}
//				}
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//
//		}
//
//		Iterator iter1 = midBaseNbr1.entrySet().iterator();
//		while (iter1.hasNext()) {
//			Map.Entry entry = (Map.Entry) iter1.next();
//			String key = (String) entry.getKey();
//			String val = (String) entry.getValue();
//			int duration = (int) (Double.parseDouble(val.split("\\|",-1)[1])-Double.parseDouble(val.split("\\|",-1)[0]));
//			
//			String newKey = key.split("\\|",-1)[0];
//			String newVal = key.split("\\|",-1)[1]+"|"+key.split("\\|",-1)[2]+"|"+duration;
//			
//			if(DirectorySnapshotRealtime.baseNbrArea.containsKey(newKey)){
//				int durationOld = Integer.parseInt(DirectorySnapshotRealtime.baseNbrArea.get(newKey).split("\\|", -1)[2]);
//				if(duration-durationOld>0){
//					DirectorySnapshotRealtime.baseNbrArea.put(newKey, newVal);
//				}
//			}else{
//				DirectorySnapshotRealtime.baseNbrArea.put(newKey, newVal);
//			}
//			
//		}
//		midBaseNbr1.clear();
		
		
		//longitude|latitude|lac|ci:nbr_num
		Map<String, Integer> midMap = new HashMap();
		
//		//longitude|latitude|nbr:longitude|latitude|nbr----(table 1.2)
//		public static Map<String,String> nbrLongLaRes = new HashMap<String, String>();
//		//nbr:lac|ci|duration----(table 2.1)
//		public static Map<String,String> baseNbrArea = new HashMap();
		
		Iterator iter2 = DirectorySnapshotRealtime.nbrLongLaRes.entrySet().iterator();
		while (iter2.hasNext()) {
			Map.Entry entry = (Map.Entry) iter2.next();
			
			String key = (String) entry.getKey();
			String nbr = key.split("\\|",-1)[2];
			
			if(DirectorySnapshotRealtime.baseNbrArea.containsKey(nbr)){
				String valBase =  DirectorySnapshotRealtime.baseNbrArea.get(nbr);
				String newKey = key.split("\\|",-1)[0]+"|"+key.split("\\|",-1)[1]+"|"+valBase.split("\\|",-1)[0]+"|"+valBase.split("\\|",-1)[1];
				if(midMap.containsKey(newKey)){
					midMap.put(newKey, midMap.get(newKey)+1);
				}else{
					midMap.put(newKey, 1);
				}
			}
		}
		
		Iterator iter3 = midMap.entrySet().iterator();
		while (iter3.hasNext()) {
			Map.Entry entry = (Map.Entry) iter3.next();
			
			String key = (String) entry.getKey();
			int value = (int) entry.getValue();
			String lacCi = key.split("\\|",-1)[2]+"|"+key.split("\\|",-1)[3];
			if(InputProperties.basestation.containsKey(lacCi)){
				String newKey = key.split("\\|",-1)[0]+"|"+key.split("\\|",-1)[1]+"|"+InputProperties.basestation.get(lacCi).toString().split("\\|",-1)[2];
				if(DirectorySnapshotRealtime.longLaArea.containsKey(newKey)){
					DirectorySnapshotRealtime.longLaArea.put(newKey, value+DirectorySnapshotRealtime.longLaArea.get(newKey));
				}else{
					DirectorySnapshotRealtime.longLaArea.put(newKey, value);
				}
			}
		}
		
		
//		subject_result_realtime_come
		String insertTruncate = new String("TRUNCATE TABLE subject_result_realtime_come");
    	String insertQuery = new String("INSERT INTO subject_result_realtime_come(LONGITUDE,LATITUDE,AREA,NBR_NUM)VALUES(?,?,?,?)");
    	SendResultData sendResultData = new SendResultData();
    	System.out.println("!!!---"+DirectorySnapshotRealtime.longLaArea.size());
	    sendResultData.writeMysql(DirectorySnapshotRealtime.longLaArea, insertQuery,4,insertTruncate,super.name,true);
	}



}
